<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>throttle</title>
</head>

<body>
    <div class="main" style="height:2000px;width:100%;"></div>
    <script>
        // 节流throttle代码（时间戳+定时器）：
        var throttle = function (func, delay) {
            var timer = null;
            var startTime = Date.now();
            return function () {
                var curTime = Date.now();
                var remaining = delay - (curTime - startTime);
                clearTimeout(timer);
                if (remaining <= 0) {
                    func()
                    startTime = Date.now();
                } else {
                    timer = setTimeout(func, remaining);
                }
            }
        }

        function handle() {
            console.log(Math.random());
        }
        window.addEventListener('scroll', throttle(handle, 1000));
    </script>
</body>

</html>